/**
 * RandomAlgo class 
 * 
 * 
 * @created 2007-04-17
 * @author csamson
 */

package sleeping;

import objects.*;
import kernel.*;
import java.lang.Math;
import java.util.*;
import java.awt.event.*;
import java.awt.*;


public class RandomAlgo extends SleepingAlgo{

    //The initial maximum coverage of the sensorNetwork
    private int _mCover;
    //The targeted coverage of the sensorNetwork
    private int _kCover;
 
    /**
     * RandomAlgo constructor
     */
    public RandomAlgo(){
    }


    /**
     * @param sn the targeted SensorNetwork
     */
    public void setSensorNetwork(SensorNetwork sn){
	_sn = sn;
	_kCover = _sn.getKCover();
	_mCover = _sn.getMaxCover();
    }
    
    /**
     * @return the algorithm name
     */
    public String getName(){
	return "RandomAlgo";
    }
    

    /**
     * calculates the sleeping for the different Sensor of the
     * current sensorNetwork
     * 
     */
    public void calculateSleeping(){
	_nbAsleep=0;
	
	float proba = (float)_kCover/(float)_mCover;
	
	Random random = new Random();
	float rand = 0;
	Vector<Sensor> sensorVect = _sn.getSensorVect();
	for(int i=0; i < sensorVect.size() ; i++){
	    rand = random.nextFloat();
		
	    if(rand <= proba){
		// The Sensor goes to bed
		_sn.setSleeping(sensorVect.get(i),false);
	    }else{
		_sn.setSleeping(sensorVect.get(i),true);
		_nbAsleep++;
	    }
	}
    }
	

    /**
     * @param m the maximum coverage of the sensorNetwork
     */	
    public void setMCouv(int m){
	_mCover = m;
    }


    /**
     * @param k the targeted coverage of the sensorNetwork
     */	
    public void setKCouv(int k){
	_kCover = k;
    }

}
